﻿using System;
using System.Collections;
using System.Data;
using System.Reflection;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using AnyOffice.WorkFlowRegex;
using log4net;
using PickListControl;
namespace AnyOffice.general.system.workflow.flow_type
{
    public partial class type_process_new : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["flow_id"] == null) ? 0 : int.Parse(Request.QueryString["flow_id"].ToString());
                this.ViewState["FlowId"] = num;
                bool flag = Request.QueryString["graph"] != null;
                this.ViewState["IsGraph"] = flag;
                if (flag)
                {
                    this.litButton.Text = "<input type=\"button\" value=\"关闭\" class=\"BigButton\" name=\"back\" onClick=\"window.close();\">";
                }
                else
                {
                    this.litButton.Text = "<input type=\"button\" value=\"返回\" class=\"BigButton\" name=\"back\" onClick=\"history.back();\">";
                }
                DataTable table = new FlowProcessDao().SelectByFlowId(base.Conn, num);
                if (table.Rows.Count > 0)
                {
                    ListItem item;
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        item = new ListItem(table.Rows[i]["PrcsId"].ToString() + "、" + table.Rows[i]["PrcsName"].ToString(), table.Rows[i]["PrcsId"].ToString());
                        this.pklMyTable.ItemsRight.Add(item);
                    }
                    item = new ListItem("[结束流程]", "0");
                    this.pklMyTable.ItemsRight.Add(item);
                    int num3 = Convert.ToInt32(table.Rows[table.Rows.Count - 1]["PrcsId"].ToString());
                    this.txtPrcsId.Text = Convert.ToString((int)(num3 + 1));
                }
                else
                {
                    this.txtPrcsId.Text = "1";
                }
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                FlowProcessDao dao2 = new FlowProcessDao();
                FlowProcessData data = new FlowProcessData();
                data.FlowId = Convert.ToInt32(this.ViewState["FlowId"].ToString());
                data.PrcsId = Convert.ToInt32(this.txtPrcsId.Text);
                data.PrcsName = this.txtPrcsName.Text;
                string str = "";
                foreach (ListItem item2 in this.pklMyTable.ItemsLeft)
                {
                    if (str == string.Empty)
                    {
                        str = item2.Value;
                    }
                    else
                    {
                        str = str + "," + item2.Value;
                    }
                }
                if (str != string.Empty)
                {
                    data.PrcsTo = str;
                }
                data.AttachmentFlag = Convert.ToInt32(this.ddlAttachmentFlag.SelectedValue);
                data.Feedback = Convert.ToInt32(this.Feedback.Value);
                data.SignLook = Convert.ToInt32(this.SignLook.Value);
                data.TurnPriv = Convert.ToInt32(this.TurnPriv.Value);
                if (this.MailTo.Value != string.Empty)
                {
                    data.MailTo = this.MailTo.Value;
                }
                data.UserFilter = Convert.ToInt32(this.UserFilter.Value);
                data.AutoType = Convert.ToInt32(this.AutoType.Value);
                data.UserLock = Convert.ToInt32(this.UserLock.Value);
                data.TopDefault = this.TopDefault.Checked;
                if (this.AutoUserOp.Value != string.Empty)
                {
                    data.AutoUserOp = this.AutoUserOp.Value;
                }
                if (this.AutoUser.Value != string.Empty)
                {
                    data.AutoUser = this.AutoUser.Value;
                }
                if (this.txtTimeout.Text != string.Empty)
                {
                    data.TimeOut = Convert.ToInt32(this.txtTimeout.Text);
                }
                if (this.txtPlugin.Text != string.Empty)
                {
                    data.Plugin = this.txtPlugin.Text;
                }
                data.PrcsDept = "ALL_DEPT";
                try
                {
                    string str2 = "[A@],";
                    FormRegex regex = new FormRegex();
                    DataTable table2 = new FlowFormTypeDao().SelectByFlowId(base.Conn, Convert.ToInt32(this.ViewState["FlowId"].ToString()));
                    if ((table2.Rows.Count > 0) && (table2.Rows[0]["FormModel"] != DBNull.Value))
                    {
                        regex.HtmlSource = table2.Rows[0]["FormModel"].ToString();
                    }
                    ArrayList titleValue = regex.GetTitleValue();
                    for (int j = 0; j < titleValue.Count; j++)
                    {
                        str2 = str2 + titleValue[j].ToString() + ",";
                    }
                    data.PrcsItem = str2;
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
                dao2.Insert(base.Conn, data);
                base.CloseConn();
                if (Convert.ToBoolean(this.ViewState["IsGraph"].ToString()))
                {
                    if (!this.Page.IsStartupScriptRegistered("SefClose"))
                    {
                        this.Page.RegisterStartupScript("SefClose", "<script>parent.opener.location.reload();window.close();</script>\n");
                    }
                }
                else
                {
                    Response.Redirect("type_view_list.aspx?flow_id=" + this.ViewState["FlowId"].ToString());
                }
            }

        }
    }
}
